﻿USE master
PRINT '-- Enable CLR'
IF EXISTS (SELECT value FROM sys.configurations WHERE name='clr enabled' AND value=0)
	EXEC ('EXEC sp_configure ''show advanced options'',1;RECONFIGURE;EXEC sp_configure ''clr enabled'',1;RECONFIGURE')
GO
PRINT '-- Create database'
IF EXISTS (SELECT 1 FROM sys.databases WHERE name='Grunndatakvalitet')
	DROP DATABASE Grunndatakvalitet
--SETUSER 'PC32751\ServiceA'
--EXEC xp_cmdshell 'cmd /csqlcmd -Q"CREATE DATABASE Grunndatakvalitet"','no_output'
GO
CREATE DATABASE Grunndatakvalitet
GO
SETUSER
ALTER DATABASE Grunndatakvalitet SET TRUSTWORTHY ON
GO
USE Grunndatakvalitet
GO
PRINT '-- Create low-level functions'
GO
CREATE SCHEMA GormClr
GO
CREATE ASSEMBLY GormClr_GrunndataExternal 
	--FROM 'F:\source_un\Grunndatakvalitet\Grunndatakvalitet_Sql_Altinn\bin\Debug\Grunndatakvalitet_Sql_Altinn.dll'
	FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C01030015B0BA530000000000000000E00002210B010B000030000000060000000000003E4F000000200000006000000000001000200000000200000400000000000000060000000000000000A000000002000000000000030060850000100000100000000010000010000000000000100000000000000000000000E84E00005300000000600000A803000000000000000000000000000000000000008000000C000000B04D00001C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000442F0000002000000030000000020000000000000000000000000000200000602E72737263000000A8030000006000000004000000320000000000000000000000000000400000402E72656C6F6300000C0000000080000000020000003600000000000000000000000000004000004200000000000000000000000000000000204F0000000000004800000002000500382F0000781E00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000133001000B000000010000110072010000700A2B00062A0013300800170000000200001100020304050E040E050E060E07280D0000060A2B00062A00133002001B0000000100001100731400000A02281500000A731600000A6F1700000A0A2B00062A00133004000F00000003000011000203040528080000060A2B00062A0003300D001E0000000000000000020304050E040E050E060E070E080E090E0A0E0B0E0C280A000006002A0000133002000D0000000400001100020328090000060A2B00062A1E02281800000A2A0000001B300900D804000005000011027B060000040D09450C00000022000000020000000400000009000000240000000E00000013000000240000002400000018000000240000001D0000002B222B653817010000387801000038F2010000386002000038BD03000038460400002B05386304000002157D0600000400027B070000047221000070281E00000A16FE01130411042D2B02027B0A00000473100000067D0500000402177D06000004170CDD2F04000002157D0600000438160400000002027B070000047223000070722B000070027B080000041414027C0C000004027C0B000004280E0000067D0D000004027B090000042C15027B090000047221000070281E00000A16FE012B011600130411042D630002723D000070027B0C00000473110000067D0E000004027B0E000004178D200000011305110516027B0B0000046F1700000AA211056F130000060002027B0E0000047D0500000402187D06000004170CDD7803000002157D0600000400385D030000027B0A0000042C15027B0A0000047221000070281E00000A16FE012B011600130411043AD60000000002724D000070027B0C00000473110000067D0F00000402027B0F0000047D0500000402197D06000004170CDD1203000002157D060000040002027B0F000004027B0B000004027B090000046F160000066F1F00000A7D16000004021A7D060000042B5402027B160000046F2000000A7D1000000400027B0F000004178D200000011305110516027B10000004A211056F130000060002027B0F0000047D05000004021B7D06000004170CDD93020000021A7D0600000400027B160000046F1900000A130411042D9B02281E0000060000385F020000027B0A0000047255000070281E00000A16FE01130411043AA001000000027267000070027B0C00000473110000067D1100000402027B110000047D05000004021C7D06000004170CDD2002000002157D0600000402732100000A7D1200000400027B11000004027B0B000004027B090000046F160000066F1F00000A13062B6F11066F2000000A0A00027B11000004066F170000066F1F00000A13072B2D11076F2000000A0B027B12000004076F2200000A15FE0116FE01130411042D0D027B12000004076F2300000A0011076F1900000A130411042DC6DE14110714FE01130411042D0811076F1C00000A00DC0011066F1900000A130411042D84DE14110614FE01130411042D0811066F1C00000A00DC0002027B120000046F2400000A7D13000004027B11000004198D20000001130511051672B3000070027B13000004282500000AA211051772B700007072F5000070027B13000004282500000A722F010070282600000AA21105187265010070729B010070027B13000004282500000A72C5010070282600000AA211056F130000060002027B110000047D05000004021F097D06000004170CDDBE00000002157D060000040038A30000000002027B0A000004027B0C00000473110000067D140000040002027B14000004027B0B000004027B090000046F160000066F1F00000A7D17000004021F0A7D060000042B4502027B170000046F2000000A7D1500000400027B14000004027B150000046F140000062602027B140000047D05000004021F0B7D06000004170CDE30021F0A7D0600000400027B170000046F1900000A130411042DAA02281F00000600000000160CDE0802281B00000600DC00082A414C000002000000040300003E00000042030000140000000000000002000000E6020000800000006603000014000000000000000400000000000000CD040000CD0400000800000000000000133001000B00000006000011027B050000040A2B00062A1A732800000A7A00001B3002005600000007000011027B060000040C081A59450200000004000000020000002B042B042B022B0BDE0802281E00000600DC00027B060000040C081F0A59450200000004000000020000002B042B042B022B0BDE0802281F00000600DC002A0000011C000002001F00022100080000000002004A00024C000800000000133001000B00000006000011027B050000040A2B00062A3A02281800000A02037D060000042A000013300200210000000800001102157D06000004027B1600000414FE010A062D0C027B160000046F1C00000A002A00000013300200210000000800001102157D06000004027B1700000414FE010A062D0C027B170000046F1C00000A002A00000013300200290000000900001116731D0000060A06027D0700000406037D0800000406047D0900000406057D0A000004060B2B00072A000000133002000D0000000400001100020328150000060A2B00062A00000013300300AE0000000A000011000274050000020A03067B030000045504067B04000004282900000A81030000010506166F1200000681030000010E0406176F1200000681030000010E0506186F1200000681030000010E0606196F1200000681030000010E07061A6F1200000681030000010E08061B6F1200000681030000010E09061C6F1200000681030000010E0A061D6F1200000681030000010E0B061E6F1200000681030000010E0C061F096F1200000681030000012A1E02281800000A2A000013300400810000000B00001100731400000A0A066F2A00000A7203020070721D0200706F2B00000A00732C00000A0B06723F0200706F2D00000A0C06729502007072E5020070076F2E00000A6F2F00000A0D0672ED0200707243030070076F2E00000A6F2F00000A2606724B03007072A1030070076F2E00000A6F2F00000A1304722100007013052B0011052A00000013300800220000000C00001100020304050E040E050E061200280E0000060B0E07066F1700000A51070C2B00082A000013300400F00000000D0000110002283000000A74270000010A06036F3100000A00067E3200000A6F3300000A00042C130472AF0300706F3400000A15FE0216FE012B011700130411042D0C067E3500000A6F3300000A000672B70300706F3600000A000E042C0F0E046F3700000A16FE0216FE012B011700130411042D09060E046F3600000A000E052C0C0E058E6916FE0216FE012B011700130411042D2A00060E058E696A6F3800000A00066F3900000A0B070E05160E058E696F3A00000A00076F3B00000A0000066F3C00000A742B0000010C0E06086F3D00000A6F3E00000A510E07086F3F00000A731600000A51086F4000000A0D2B00092A1E02281800000A2A133004008A0000000E00001102281800000A000002166A7D030000040314FE0116FE010B072D0E02168D200000017D010000042B17020372B30000706F4100000A176F4200000A7D0100000402027B010000048E698D030000017D02000004160A2B22027B02000004068F03000001027B01000004069A734300000A81030000010617580A06027B010000048E69FE040B072DCF002A4A02032810000006000002047D04000004002A00000013300200310000000F0000110003027B010000048E69FE0416FE010B072D14027B02000004038F0300000171030000010A2B087E4400000A0A2B00062A000000133003003B0000000E0000110002038E698D030000017D02000004160A2B1D027B02000004068F0300000103069A734300000A81030000010617580A06038E69FE040B072DD92A0013300400E30000001000001100027B020000048E6916FE0116FE010C082D410002178D030000010D09168F0300000103282900000A8103000001097D0200000402178D20000001130411041672C9030070A211047D01000004170B388D0000000002257B03000004176A587D03000004160A2B23027B02000004068F0300000103027B01000004069A281500000681030000010617580A06027B020000048E69FE040C082DCE160A2B2F027B02000004068F0300000171030000017E4400000A284500000A284600000A16FE010C082D04170B2B180617580A06027B020000048E69FE040C082DC200160B2B00072A00133005008E00000011000011007E4400000A0A0272D903007003284700000A6F3400000A0B0716FE04130411042D6500021F3E076F4800000A17580B0716FE0216FE01130411042D4A000272DD0300700372E3030070282600000A0717586F4900000A0C0816FE0216FE01130411042D21120002070807596F4A00000A72E70300706F4100000A6F4B00000A284300000A000000060D2B00092A0000133006002D0000001200001100036F1700000A178D200000010B071672DD0300700472E3030070282600000AA207176F4C00000A0A2B00062A000000133002005200000013000011284E00000A027B1A000004330F027B190000041FFEFE0116FE012B0117000C082D0B02167D19000004020A2B131673270000060A06027B1B0000047D1B00000406027B1D0000047D1C000004060B2B00072A0000133001000B000000030000110228200000060A2B00062A0013300500E400000014000011027B190000040B07450200000007000000020000002B07388B0000002B0538BB00000002157D190000040002027B1C00000472DD0300706F3400000A7D1E00000438870000000002027B1C0000041F3E027B1E00000417586F4800000A7D1F000004027B1F00000415FE0116FE010C082D022B6902027B1C000004027B1E0000041858027B1F000004027B1E0000045918596F4A00000A7D1800000402177D19000004170A2B3B02157D1900000402027B1C00000472DD030070027B1E00000417586F4900000A7D1E00000400027B1E00000415FE020C083A69FFFFFF00160A2B00062A133001000B00000001000011027B180000040A2B00062A1A732800000A7A0A002A000000133001000B00000006000011027B180000040A2B00062A6602281800000A02037D1900000402284E00000A7D1A0000042A000000133002001C000000150000111FFE73270000060A06027D1B00000406037D1D000004060B2B00072A42534A4201000100000000000C00000076342E302E33303331390000000005006C000000600A0000237E0000CC0A0000D80B000023537472696E677300000000A4160000F003000023555300941A0000100000002347554944000000A41A0000D403000023426C6F6200000000000000020000015717A20B0902000000FA2533001600000100000031000000070000001F000000270000004C000000080000004F0000001F000000150000000200000004000000040000000C0000000400000001000000030000000400000000000A00010000000000060084007D000600C400B1000A000F01FA00060069015F010600C701AC01060066024702060024030A0306004F033D03060066033D03060083033D030600A2033D030600BB033D030600D4033D030600EF033D0306000A043D03060023044702060037044702060045043D0306005E043D0306008E047B045300A20400000600D104B1040600F104B1040A002A050F050A003F050F050E006005550506006A055F0106007A055F010600B305AC010600C1057D0006001007AC010600E1077D00060022087B0406003A087D000E005C0855050E009B087C080600B8085F010E00DD0855050E00EF0855050E0009095505060015097D000E008F0955050E00A70955050E00D20955050600FC097D000A001A0AFA0006005B0AB1000600960B7D000600BD0BB1040000000001000000000001000100010010002B000000050001000100010010003D000000050001000800020010005A000000050001000C00020010005F000000050001001000030110008F050000050005001800030110004A0A000005001800200004007601720004008001760006008B017B0006008E017E000100D605240101007F0630010600EA017E00060084027E000600C8027E00060095027E000600B50633010600C5067E000600DC0630010600E70637010600F10637010600FB067E00060006073701060017073B0106002607720006003507370106003F077E0006004A0742010600630742010100D6057E0001007F06300101001A0B300106002F0B3701060005037E000600390B7E000600430B300106004C0B300150200000000096008B000A000100682000000000960096000E0001008C20000000009600A2001D000900B420000000009600D00022000A00D02000000000960019012B000E00FC20000000009600360153001B00152100000000861848015A001D007827000000009600D00022001D00B027000000009600360153002100CC2700000000960019012B002300862800000000861848015A00300090280000000081004E015E003000202900000000960058010E00300050290000000096005801620038004C2A00000000861848015A004000542A000000008618480181004000EA2A000000008618480186004100002B00000000860096018C004300402B000000008600A20192004400882B000000008600A20198004500782C000000009600360153004600142D000000008600D5019D004800102F000000008600E001A8004A00202100000000E101CD0520014B00502600000000E109E30527014B00672600000000E10131065A004B00702600000000E1015C065A004B00F02600000000E1098A0627014B0007270000000086184801BC004B00182700000000810055075A004C0048270000000081006E075A004C00502D00000000E101670AAF024C00B02D00000000E101AB0AB7024C00C82D00000000E101CD0520014C00B82E00000000E109D80A5E004C00CF2E00000000E10131065A004C00D62E00000000E1015C065A004C00DC2E00000000E1098A0627014C00F32E0000000086184801BC004C0000000100EA0100000200EF0100000300F70100000400080200000500180200000600250202000700360202000800730200000100EA0100000100EA01000002008402000003008D02000004009502000001009E02020002008B01020003008E0102000400A20202000500A50202000600A80202000700AB0202000800AE0202000900B10202000A00B40202000B00B70202000C00BA0202000D00BD02000001008D0200000200C00200000100EA0100000200840200000300C802000004009502000001008D0200000200C002000001009E02020002008B01020003008E0102000400A20202000500A50202000600A80202000700AB0202000800AE0202000900B10202000A00B40202000B00B70202000C00BA0202000D00BD0200000100EA0100000200EF0100000300F70100000400080200000500180200000600D00202000700360202000800730200000100EA0100000200EF0100000300F70100000400080200000500180200000600D00202000700360202000800DE02000001009502000001009502000002008E0100000100F00200000100F202000001008D02000001008D0200000200C00200000100FB0200000200C802000001000503000001007F06000001007F0606000600060009000600790007000A000700BD0007000E000700090007007900310048015A0039004801810041004801810049004801810051004801810059004801810061004801810069004801810071004801810079004801810081004801B100890048018100910048018100990048018100A1004801B600B1004801BC00B90048015A00C10048015A00C90048015A00D10048015A00D1007105CE0021004801D400E10085055E00090048015A001100CD0520010C0025062B01110056065A00F10077065A001100250627010101E8074D011400F40759011C0025062B01240048015A00240002086E0124000A08740124000E087A0101011608800101011B088701090148015A00110148015A0019005008B701D1007008C2012101AF088600290148015A00D100C508C80129010E08CE01D100D208D3013101E808F2013101FE08810041011D09F90139012709FE0101010208050241013B09F901310145098100010155090A02310160090E02310172091302D90083091802D90089095A0031019B09200251017008C2010900B7095E005101C00913025901E10926020101F009390201010F0A3E021900480181001900150A4D021900250A57027101330A610201011B087302010102087902010102087F0201013B0A85020101450A8B0201010F0A9B027901F407B7028101A20BC002890148015A0020009300C1002E008B00B2032E002B004B032E0033004B032E008300A9032E003B0029032E001300DF022E001B0029032E0023004B032E004B004B032E005B004B032E007B00A0032E00430051032E00630069032E007300930340009B00C10060009300C10080009300DA00C0009300C100C3007B02C100E3007B02C10020033B01C10040033B01C10080033B01C100A0033B01C10000043B01C10020043B01C10060043B01C10080043B01C100C0043B01C100E0043B01C100C600CA00100115018E01A201A601AC01B001BD01DC01EB012C024802510268029102A502C402CF02D502060001000700030000007C0749010000BA0749010000580BBC020000BA07490102001900030002001C000500020023000700020026000900060030003300060032003500060034003700060036003900060038003B00070040003F00070042009B0007004400330007004600410007004800370007004A00390007004C003B001A015301620168010480000001000000000000000000000000003D00000004000000000000000000000001007400000000000400000000000000000000000100EE000000000004000000000000000000000001007D0000000000040003000500030006000300070005000000003C4D6F64756C653E004772756E6E646174616B76616C697465745F53716C5F416C74696E6E2E646C6C004772756E6E646174616B76616C69746574004772756E6E646174616B76616C697465745F53716C5F416C74696E6E004874747000476574526F7773416E644669656C64735F526F77006D73636F726C69620053797374656D004F626A6563740048656C6C6F576F726C6400487474705F4D6574686F6400487474705F53696D706C654765740053797374656D2E436F6C6C656374696F6E730049456E756D657261746F7200487474705F53696D706C65476574526F7773416E644669656C647331300053797374656D2E446174610053797374656D2E446174612E53716C54797065730053716C537472696E6700487474705F476574526F7773416E644669656C647331305F46696C6C00457874726163744669656C6456616C7565002E63746F7200536F6D6546756E6374004D6574686F640053797374656D2E494F0053747265616D5265616465720073436F6C4E616D65730073436F6C56616C75657300706B006865616465727300476574436F6C56616C75650053657456616C7565730053797374656D2E436F6C6C656374696F6E732E47656E657269630049456E756D657261626C6560310046726F6D53747265616D0054616773466F756E64007355726900734D6574686F64007350726F746F636F6C56657273696F6E007352657175657374486561646572730073436F6E74656E745479706500626152657175657374436F6E74656E740073526573706F6E7365486561646572730053797374656D2E52756E74696D652E496E7465726F705365727669636573004F75744174747269627574650073526573706F6E7365436F6E74656E740073486561646572730073526F775461670073436F6C5461677300726F770073300073310073320073330073340073350073360073370073380073390073436F6C5461670073526F7774616700626152657175657374426F6479007372526573706F6E7365436F6E74656E74006900736C56616C756573007372436F6E74656E740073526F770053797374656D2E52756E74696D652E56657273696F6E696E67005461726765744672616D65776F726B4174747269627574650053797374656D2E5265666C656374696F6E00417373656D626C795469746C6541747472696275746500417373656D626C794465736372697074696F6E41747472696275746500417373656D626C79436F6E66696775726174696F6E41747472696275746500417373656D626C79436F6D70616E7941747472696275746500417373656D626C7950726F6475637441747472696275746500417373656D626C79436F7079726967687441747472696275746500417373656D626C7954726164656D61726B41747472696275746500417373656D626C7943756C7475726541747472696275746500436F6D56697369626C65417474726962757465004775696441747472696275746500417373656D626C7956657273696F6E41747472696275746500417373656D626C7946696C6556657273696F6E4174747269627574650053797374656D2E446961676E6F73746963730044656275676761626C6541747472696275746500446562756767696E674D6F6465730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C697479417474726962757465004D6963726F736F66742E53716C5365727665722E5365727665720053716C46756E6374696F6E4174747269627574650053716C50726F6365647572654174747269627574650053797374656D2E4E657400576562436C69656E740053747265616D004F70656E5265616400546578745265616465720052656164546F456E64003C487474705F53696D706C65476574526F7773416E644669656C647331303E645F5F300049456E756D657261746F7260310049446973706F7361626C65004D6F76654E657874003C3E325F5F63757272656E740053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261746F723C53797374656D2E4F626A6563743E2E6765745F43757272656E74006765745F43757272656E740053797374656D2E436F6C6C656374696F6E732E49456E756D657261746F722E52657365740052657365740053797374656D2E49446973706F7361626C652E446973706F736500446973706F7365003C3E315F5F73746174650053797374656D2E436F6C6C656374696F6E732E49456E756D657261746F722E6765745F43757272656E74003C7372436F6E74656E743E355F5F31003C73526573706F6E7365486561646572733E355F5F32003C695265733E355F5F33003C726F773E355F5F34003C726F773E355F5F35003C73526F773E355F5F36003C726F773E355F5F37004C6973746031003C6C734669656C64733E355F5F38003C73614669656C64733E355F5F39003C726F773E355F5F61003C73526F773E355F5F62003C3E375F5F7772617063003C3E6D5F5F46696E616C6C7964003C3E375F5F7772617065003C3E6D5F5F46696E616C6C79660053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261746F723C53797374656D2E4F626A6563743E2E43757272656E740053797374656D2E436F6C6C656374696F6E732E49456E756D657261746F722E43757272656E7400537472696E67006F705F457175616C69747900476574456E756D657261746F7200496E6465784F660041646400546F4172726179004A6F696E00436F6E63617400446562756767657248696464656E417474726962757465004E6F74537570706F72746564457863657074696F6E006F705F496D706C6963697400576562486561646572436F6C6C656374696F6E006765745F486561646572730053797374656D2E436F6C6C656374696F6E732E5370656369616C697A6564004E616D6556616C7565436F6C6C656374696F6E007365745F4974656D004D656D6F727953747265616D00446F776E6C6F6164446174610055706C6F616444617461005765625265717565737400437265617465004874747057656252657175657374007365745F4D6574686F64004874747056657273696F6E0056657273696F6E0056657273696F6E3130007365745F50726F746F636F6C56657273696F6E0056657273696F6E3131007365745F436F6E74656E7454797065006765745F4C656E677468007365745F436F6E74656E744C656E677468004765745265717565737453747265616D00577269746500436C6F736500576562526573706F6E736500476574526573706F6E73650048747470576562526573706F6E736500546F537472696E6700476574526573706F6E736553747265616D0048747470537461747573436F6465006765745F537461747573436F646500546F43686172417272617900537472696E6753706C69744F7074696F6E730053706C6974004E756C6C0053716C426F6F6C65616E006F705F496E657175616C697479006F705F5472756500537562737472696E67005472696D003C54616773466F756E643E645F5F31310049456E756D657261626C650053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261626C653C53797374656D2E537472696E673E2E476574456E756D657261746F720053797374656D2E436F6C6C656374696F6E732E49456E756D657261626C652E476574456E756D657261746F720053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261746F723C53797374656D2E537472696E673E2E6765745F43757272656E74003C3E6C5F5F696E697469616C5468726561644964003C3E345F5F74686973003C3E335F5F73526F77003C693E355F5F3132003C69456E643E355F5F31330053797374656D2E436F6C6C656374696F6E732E47656E657269632E49456E756D657261746F723C53797374656D2E537472696E673E2E43757272656E7400456E7669726F6E6D656E74006765745F43757272656E744D616E61676564546872656164496400436F6D70696C657247656E65726174656441747472696275746500001F480065006C006C006F00200079006F0075007200730065006C006600210000010007470045005400001148005400540050002F0031002E003100000F63006F006E00740065006E007400000772006F007700001167006500740020007400610067007300004B74006100670073007C00730071006C005F006D0065007400610064006100740061005F006300720065006100740065007C00730071006C005F00660069006E0064005F006C0065006E0000037C00003D530045004C00450043005400200054004F00500020003000200070006B003D0030002C0068006500610064006500720073003D00270027002C00200001393D00430041005300540028002700270020004100530020004E00560041005200430048004100520028004D0041005800290029002C00200001353D00430041005300540028002700270020004100530020004E00560041005200430048004100520028004D0041005800290029000135530045004C00450043005400200052006F00770043006F0075006E0074003D0043004F0055004E00540028002A0029002C00200000295F005F006C0065006E003D004D004100580028004C0045004E00280073004E00290029002C002000003D5F005F006C0065006E003D004D004100580028004C0045004E00280073004E00290029002000460052004F004D00200073002E006600280078002900001943006F006E00740065006E0074002D00740079007000650001216100700070006C00690063006100740069006F006E002F006A0073006F006E00005568007400740070003A002F002F006C006F00630061006C0068006F00730074003A00340039003100390033002F0043006F006E00740061006300740073002E007300760063002F0047006500740041006C006C00004F68007400740070003A002F002F006C006F00630061006C0068006F00730074003A00340039003100390033002F0043006F006E00740061006300740073002E007300760063002F004100640064000007410044004400005568007400740070003A002F002F006C006F00630061006C0068006F00730074003A00340039003100390033002F0043006F006E00740061006300740073002E007300760063002F005500700064006100740065000007500055005400005568007400740070003A002F002F006C006F00630061006C0068006F00730074003A00340039003100390033002F0043006F006E00740061006300740073002E007300760063002F00440065006C00650074006500000D440045004C00450054004500000731002E003100001174006500780074002F0078006D006C00000F43006F006E00740065006E00740000033C0000053C002F0000033E00000520000A00000000000076EECE06C00C4E976099B7AB7E182B0008B77A5C561934E0890300000E0E0008080E0E0E0E0E1D05100E100E0400010E0E08000412090E0E0E0E27000D011C100A10110D10110D10110D10110D10110D10110D10110D10110D10110D10110D10110D060002110D0E0E032000010320000E0F0008080E0E0E0E0E1D05100E10121103061D0E04061D110D02060A02060E042001010E052002010E0E052001110D08052001011D0E042001020E0A2002151215010E12110E082001151215010E0E0420010102052001011155042001010804010000000307010E03070108052001126D0E05200101126D3501000100540E1146696C6C526F774D6574686F644E616D651C487474705F476574526F7773416E644669656C647331305F46696C6C0407011209040701110D05151275011C0320000202061C0320001C04200013000206080306121103061214060615127D010E0606151275010E0328001C050002020E0E05151215010E08200015127501130005151275010E0515127D010E0520010813000520010113000520001D13000600020E0E1D0E0600030E0E0E0E1307080E0E0208021D0E151275010E151275010E0307011C0507030E0E080307010206070212181209050001110D0E040701121405200012808D0520011D050E0420001D050820031D050E0E1D050E070612691280951D051D051D050E060703121108080600011280990E04061280A5062001011280A5042001080E03200008042001010A042000126D072003011D0508080520001280A90520001180B10C070512809D126D1280AD08020420001D030920021D0E1D031180B504070208020306110D050702110D020900021180B9110D110D060001021180B90A07050802021D110D1D0E0500020E0E0E052002080308052002080E080520020E08080520010E1D03090705110D0808110D020920021D0E1D0E1180B5090702151215010E1D0E072000151275010E04200012090328000E030000080A0703121C151275010E02050703020802090702121C151215010E4901001A2E4E45544672616D65776F726B2C56657273696F6E3D76342E350100540E144672616D65776F726B446973706C61794E616D65122E4E4554204672616D65776F726B20342E352101001C4772756E6E646174616B76616C697465745F53716C5F416C74696E6E000005010000000017010012436F7079726967687420C2A920203230313400002901002432393138653739322D653131622D343264342D613761342D66393263343234636464353700000C010007312E302E302E3000000801000701000000000801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F7773010000000000000015B0BA5300000000020000001C010000CC4D0000CC2F000052534453F4933F96117BB34AB6E7D657DA99B44703000000663A5C736F757263655F756E5C4772756E6E646174616B76616C697465745C4772756E6E646174616B76616C697465745F53716C5F416C74696E6E5C6F626A5C44656275675C4772756E6E646174616B76616C697465745F53716C5F416C74696E6E2E7064620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000104F000000000000000000002E4F0000002000000000000000000000000000000000000000000000204F000000000000000000000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF2500200010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100100000001800008000000000000000000000000000000100010000003000008000000000000000000000000000000100000000004800000058600000500300000000000000000000500334000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000001000000000000000100000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004B0020000010053007400720069006E006700460069006C00650049006E0066006F0000008C020000010030003000300030003000340062003000000064001D000100460069006C0065004400650073006300720069007000740069006F006E00000000004700720075006E006E0064006100740061006B00760061006C0069007400650074005F00530071006C005F0041006C00740069006E006E0000000000300008000100460069006C006500560065007200730069006F006E000000000031002E0030002E0030002E003000000064002100010049006E007400650072006E0061006C004E0061006D00650000004700720075006E006E0064006100740061006B00760061006C0069007400650074005F00530071006C005F0041006C00740069006E006E002E0064006C006C00000000004800120001004C006500670061006C0043006F007000790072006900670068007400000043006F0070007900720069006700680074002000A90020002000320030003100340000006C00210001004F0072006900670069006E0061006C00460069006C0065006E0061006D00650000004700720075006E006E0064006100740061006B00760061006C0069007400650074005F00530071006C005F0041006C00740069006E006E002E0064006C006C00000000005C001D000100500072006F0064007500630074004E0061006D006500000000004700720075006E006E0064006100740061006B00760061006C0069007400650074005F00530071006C005F0041006C00740069006E006E0000000000340008000100500072006F006400750063007400560065007200730069006F006E00000031002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000031002E0030002E0030002E003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000C000000403F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	WITH PERMISSION_SET=EXTERNAL_ACCESS
GO
CREATE FUNCTION GormClr.ExtractFieldValue(@sRowTag NVARCHAR(MAX), @sColTag NVARCHAR(4000)) RETURNS NVARCHAR(MAX) EXTERNAL NAME GormClr_GrunndataExternal.Grunndatakvalitet.ExtractFieldValue
GO--SELECT GormClr.ExtractFieldValue('<ArrayOfServiceMetaData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://schemas.altinn.no/2013/09/api"><Links xmlns:d3p1="http://schemas.datacontract.org/2004/07/Altinn.SBL.AltinnWebAPI.Models"/><RestEnabled i:nil="true"/><ServiceCode>3736</ServiceCode><ServiceEditionCode>140122</ServiceEditionCode><ServiceName>A06 a-melding bestill avstemmingsinformasjon</ServiceName><ServiceOwnerCode>SKD</ServiceOwnerCode><ServiceOwnerName>Skattedirektoratet</ServiceOwnerName><ServiceType>FormTask</ServiceType><ValidFrom>2014-05-05T08:00:00</ValidFrom><ValidTo>2999-01-31T13:00:00</ValidTo></ServiceMetaData>','ServiceType')
CREATE FUNCTION GormClr.Http_SimpleGet(@sUri NVARCHAR(MAX)) RETURNS NVARCHAR(MAX) EXTERNAL NAME GormClr_GrunndataExternal.Grunndatakvalitet.Http_SimpleGet
GO-- SELECT GormClr.Http_SimpleGet('https://twitter.com/'),GormClr.Http_SimpleGet('http://altinnett.brreg.no/api/metadata?')GormClr.Http_SimpleGet('https://altinnett.brreg.no/api/metadata?')
CREATE FUNCTION GormClr.Http_SimpleGetRowsAndFields10(@sUri NVARCHAR(MAX), @sHeaders NVARCHAR(MAX), @sRowTag NVARCHAR(4000), @sColTags NVARCHAR(4000))
	RETURNS TABLE (PK BIGINT, headers NVARCHAR(MAX), s0 NVARCHAR(MAX), s1 NVARCHAR(MAX), s2 NVARCHAR(MAX), s3 NVARCHAR(MAX), s4 NVARCHAR(MAX), s5 NVARCHAR(MAX), s6 NVARCHAR(MAX), s7 NVARCHAR(MAX), s8 NVARCHAR(MAX), s9 NVARCHAR(MAX))
	EXTERNAL NAME GormClr_GrunndataExternal.Grunndatakvalitet.Http_SimpleGetRowsAndFields10
GO
CREATE PROC GormClr.Http_Method @sUri NVARCHAR(MAX), @sMethod NVARCHAR(MAX), @sProtocolVersion NVARCHAR(MAX), @sHeaders NVARCHAR(MAX), @sContentType NVARCHAR(MAX), @baContent VARBINARY(4000), @sResponseHeaders NVARCHAR(4000) OUTPUT, @sResponseContent NVARCHAR(MAX) OUTPUT AS
	EXTERNAL NAME GormClr_GrunndataExternal.Grunndatakvalitet.Http_Method
GO--DECLARE @ret INT, @sResponseHeaders NVARCHAR(4000), @sResponseContent NVARCHAR(MAX);--EXEC @ret=GormClr.Http_Method 'http://www.google.no', 'GET', NULL, NULL, NULL, @sResponseHeaders OUT, @sResponseContent OUT;PRINT @ret;PRINT @sResponseHeaders;PRINT @sResponseContent
PRINT '-- Aquire'
EXEC ('CREATE SCHEMA Aquire')
EXEC ('CREATE FUNCTION Aquire.h(@v NVARCHAR(MAX),@n INT) RETURNS BIGINT WITH SCHEMABINDING AS BEGIN DECLARE @r BIGINT=HASHBYTES(''MD5'',ISNULL(CAST(@n AS SYSNAME),'''')+ISNULL(@v,''{null}''));SET @r-=@n*SIGN(@r);RETURN @r; END')
EXEC ('CREATE FUNCTION Aquire.h1(@v1 NVARCHAR(MAX)) RETURNS BIGINT WITH SCHEMABINDING AS BEGIN RETURN Aquire.h(@v1,1) END')
EXEC ('CREATE FUNCTION Aquire.h2(@v1 NVARCHAR(MAX),@v2 NVARCHAR(MAX)) RETURNS BIGINT WITH SCHEMABINDING AS BEGIN RETURN Aquire.h(@v1,1)^Aquire.h(@v2,2) END')
EXEC ('CREATE FUNCTION Aquire.h3(@v1 NVARCHAR(MAX),@v2 NVARCHAR(MAX),@v3 NVARCHAR(MAX)) RETURNS BIGINT WITH SCHEMABINDING AS BEGIN RETURN Aquire.h(@v1,1)^Aquire.h(@v2,2)^Aquire.h(@v3,3) END')
GO-- Define request type
EXEC ('CREATE TABLE Aquire.RequestType(Code BIGINT IDENTITY(1,1), Name SYSNAME, Uri NVARCHAR(4000), Headers NVARCHAR(4000), RegisterFrequency BIGINT DEFAULT 1, FirstRegisteredDate DATETIME DEFAULT GETDATE(), Delta AS Aquire.h2(Uri,Headers) PERSISTED)')
GO

--*/
/*-- Request content
--*/
/*-- Get row candidates
--*/
/*-- Get columns
--*/
-- Store content
--SELECT * FROM GormClr.Http_SimpleGetRowsAndFields10('https://www.altinn.no/api/metadata', NULL, NULL, '') --*/
-- Read rows
--SELECT * FROM GormClr.Http_SimpleGetRowsAndFields10('https://www.altinn.no/api/metadata', NULL, 'ServiceMetaData', NULL) --*/
-- Get tag names and commands
--SELECT * FROM GormClr.Http_SimpleGetRowsAndFields10('https://www.altinn.no/api/metadata', NULL, 'ServiceMetaData', 'get tags') --*/
-- Read all data with metadata set
--SELECT TOP 0 pk=0,ServiceCode=CAST('' AS NVARCHAR(MAX)), ServiceEditionCode=CAST('' AS NVARCHAR(MAX)), ServiceName=CAST('' AS NVARCHAR(MAX)), ServiceOwnerCode=CAST('' AS NVARCHAR(MAX)), ServiceOwnerName=CAST('' AS NVARCHAR(MAX)), ServiceType=CAST('' AS NVARCHAR(MAX)), ValidFrom=CAST('' AS NVARCHAR(MAX)), ValidTo=CAST('' AS NVARCHAR(MAX))
--		UNION ALL
--		SELECT pk,s0,s1,s2,s3,s4,s5,s6,s7 FROM GormClr.Http_SimpleGetRowsAndFields10('https://www.altinn.no/api/metadata', NULL, 'ServiceMetaData', 'ServiceCode|ServiceEditionCode|ServiceName|ServiceOwnerCode|ServiceOwnerName|ServiceType|ValidFrom|ValidTo|ArrayOfServiceMetaData') --*/
-- Create view
--SELECT TOP 0 pk=0,ServiceCode=CAST('' AS NVARCHAR(MAX)), ServiceEditionCode=CAST('' AS NVARCHAR(MAX)), ServiceName=CAST('' AS NVARCHAR(MAX)), ServiceOwnerCode=CAST('' AS NVARCHAR(MAX)), ServiceOwnerName=CAST('' AS NVARCHAR(MAX)), ServiceType=CAST('' AS NVARCHAR(MAX)), ValidFrom=CAST('' AS NVARCHAR(MAX)), ValidTo=CAST('' AS NVARCHAR(MAX))
--		UNION ALL
--		SELECT pk,s0,s1,s2,s3,s4,s5,s6,s7 FROM GormClr.Http_SimpleGetRowsAndFields10('https://www.altinn.no/api/metadata', NULL, 'ServiceMetaData', 'ServiceCode|ServiceEditionCode|ServiceName|ServiceOwnerCode|ServiceOwnerName|ServiceType|ValidFrom|ValidTo|ArrayOfServiceMetaData') --*/
GO 
--GET https://www.altinn.no/api/my/messages HTTP/1.1
--Host: www.altinn.no
--Accept: application/hal+json
--ApiKey: myKey

--GET https://www.altinn.no/api/metadata HTTP/1.1
--ApiKey: myKey
--Accept: application/hal+json

CREATE SCHEMA Altinn 
GO
CREATE VIEW Altinn.Metadata AS
SELECT TOP 0 pk=0,ServiceCode=CAST('' AS NVARCHAR(MAX)), ServiceEditionCode=CAST('' AS NVARCHAR(MAX)), ServiceName=CAST('' AS NVARCHAR(MAX)), ServiceOwnerCode=CAST('' AS NVARCHAR(MAX)), ServiceOwnerName=CAST('' AS NVARCHAR(MAX)), ServiceType=CAST('' AS NVARCHAR(MAX)), ValidFrom=CAST('' AS NVARCHAR(MAX)), ValidTo=CAST('' AS NVARCHAR(MAX))
	UNION ALL SELECT pk,s0,s1,s2,s3,s4,s5,s6,s7 
	FROM GormClr.Http_SimpleGetRowsAndFields10('https://www.altinn.no/api/metadata', NULL, 'ServiceMetaData', 'ServiceCode|ServiceEditionCode|ServiceName|ServiceOwnerCode|ServiceOwnerName|ServiceType|ValidFrom|ValidTo|ArrayOfServiceMetaData') WHERE s0 IS NOT NULL
GO

SELECT What='Test: Altinn'
SELECT * FROM Altinn.Metadata ORDER BY ValidTo
SELECT What='Test: www.google.no'
DECLARE @ret INT, @sResponseHeaders NVARCHAR(4000), @sResponseContent NVARCHAR(MAX);
EXEC @ret=GormClr.Http_Method 'http://www.google.no', 'GET', NULL, NULL, NULL, NULL, @sResponseHeaders OUT, @sResponseContent OUT;
SELECT ret=@ret,headers=@sResponseHeaders,content=@sResponseContent
--*/
